{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "import threading\n",
    "import datetime\n",
    "import time\n",
    "\n",
    "# 创建主窗口\n",
    "app = tkinter.Tk()\n",
    "app.overrideredirect(True)  # 不显示标题栏\n",
    "app.attributes('-alpha', 0.9)  # 半透明窗体\n",
    "app.attributes('-topmost', 1)  # 窗口总是在顶端显示\n",
    "app.geometry('110x25+100+100')  # 设置初始大小与位置\n",
    "\n",
    "# 创建标签用于显示日期时间\n",
    "labelDateTime = tkinter.Label(app)\n",
    "labelDateTime.pack(fill=tkinter.BOTH, expand=tkinter.YES)\n",
    "labelDateTime.configure(bg='gray')Ss\n",
    "# 变量X和Y用来记录鼠标左键按下的位置\n",
    "X = tkinter.IntVar(value=0)\n",
    "Y = tkinter.IntVar(value=0)\n",
    "\n",
    "# 表示窗口是否可拖动的变量\n",
    "canMove = tkinter.IntVar(value=0)\n",
    "\n",
    "# 表示是否仍在运行的变量\n",
    "still = tkinter.IntVar(value=1)\n",
    "\n",
    "def onLeftButtonDown(event):\n",
    "    app.attributes('-alpha', 0.4)  # 开始拖动时增加透明度\n",
    "    X.set(event.x)S\n",
    "    Y.set(event.y)\n",
    "    canMove.set(1)  # 标记窗口可拖动\n",
    "\n",
    "# 绑定鼠标左键单击事件处理函数\n",
    "labelDateTime.bind('<Button-1>', onLeftButtonDown)\n",
    "\n",
    "def onLeftButtonUp(event):\n",
    "    app.attributes('-alpha', 0.9)  # 停止拖动时恢复透明度\n",
    "    canMove.set(0)  # 鼠标左键抬起，标记窗口不可拖动\n",
    "\n",
    "# 绑定鼠标左键抬起事件处理函数\n",
    "labelDateTime.bind('<ButtonRelease-1>', onLeftButtonUp)\n",
    "\n",
    "def onLeftButtonMove(event):\n",
    "    if canMove.get() == 0:\n",
    "        return\n",
    "    # 重新计算并修改窗口的新位置\n",
    "    newX = app.winfo_x() + (event.x - X.get())\n",
    "    newY = app.winfo_y() + (event.y - Y.get())\n",
    "    g = f'110x25+{newX}+{newY}'\n",
    "    app.geometry(g)\n",
    "\n",
    "# 绑定鼠标左键移动事件处理函数\n",
    "labelDateTime.bind('<B1-Motion>', onLeftButtonMove)\n",
    "\n",
    "def onRightButtonDown(event):\n",
    "    still.set(0)\n",
    "    t.join(0.2)  # 等待线程结束\n",
    "    app.destroy()  # 关闭窗口\n",
    "\n",
    "# 绑定鼠标右键单击事件处理函数\n",
    "labelDateTime.bind('<Button-3>', onRightButtonDown)\n",
    "\n",
    "# 显示当前时间的线程函数\n",
    "def nowDateTime():\n",
    "    while still.get() == 1:\n",
    "        now = datetime.datetime.now()\n",
    "        s = now.strftime('%Y-%m-%d %H:%M:%S')  # 使用 strftime 方法简化字符串格式化\n",
    "        labelDateTime['text'] = s\n",
    "        time.sleep(0.5)\n",
    "# 创建并启动线程\n",
    "t = threading.Thread(target=nowDateTime)\n",
    "t.daemon = True\n",
    "t.start()\n",
    "\n",
    "# 进入主循环  \n",
    "app.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
